package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Article;
import com.hs.sfzy.service.impl.ArticleServiceImpl;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author zgy
 * @Date 2024年10月21日 10:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;

    /**
     * 添加数据
     *
     * @param article
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 15:34
     */
    @PostMapping("/add")
    public Result insert(@RequestBody Article article) {
        int num = articleService.insert(article);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 15:29
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        int num = articleService.delete(id);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @PutMapping("/edit")
    public Result update(@RequestBody Article article) {
        int num = articleService.update(article);
        if (num != 0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 查询所有数据
     *
     * @param article
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 15:29
     */
    @GetMapping("findAll")
    public Result selectAll(@RequestBody Article article) {
        List<Article> articles = articleService.selectAll(article);
        return Result.success(articles);
    }

    /**
     * 根据id查询数据
     *
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 15:31
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id) {
        Article article = articleService.selectById(id);
        return Result.success(article);
    }

    /**
     * 分页查询数据
     *
     * @param pageNum
     * @param pageSize
     * @param article
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 16:06
     */
    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Article article) {
        PageInfo<Article> info = articleService.selectByPageInfo(pageNum, pageSize, article);
        return Result.success(info);
    }


}

